package com.devbobcorn.donutopia.entities;

import net.minecraft.entity.CreatureAttribute;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.ai.goal.LookAtGoal;
import net.minecraft.entity.ai.goal.LookRandomlyGoal;
import net.minecraft.entity.ai.goal.MeleeAttackGoal;
import net.minecraft.entity.ai.goal.NearestAttackableTargetGoal;
import net.minecraft.entity.ai.goal.WaterAvoidingRandomWalkingGoal;
import net.minecraft.entity.monster.MonsterEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.IPacket;
import net.minecraft.util.DamageSource;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.fml.network.FMLPlayMessages;
import net.minecraftforge.fml.network.NetworkHooks;
import net.minecraftforge.registries.ForgeRegistries;

public class GingerBreadManEntity extends MonsterEntity {
    public GingerBreadManEntity(FMLPlayMessages.SpawnEntity packet, World world) {
        this(ModEntityTypes.GINGER_BREAD_MAN, world);
    }

    public GingerBreadManEntity(EntityType<GingerBreadManEntity> type, World world) {
        super(type, world);
        xpReward = 0;
        setNoAi(false);
    }

    @Override
    public IPacket<?> getAddEntityPacket() {
        return NetworkHooks.getEntitySpawningPacket(this);
    }

    protected void registerGoals() {
        this.goalSelector.addGoal(8, new LookAtGoal(this, PlayerEntity.class, 8.0F));
        this.goalSelector.addGoal(8, new LookRandomlyGoal(this));
        this.applyEntityAI();
    }
  
     protected void applyEntityAI() {
        this.goalSelector.addGoal(2, new AttackGoal(this));
        this.goalSelector.addGoal(7, new WaterAvoidingRandomWalkingGoal(this, 1.0D));
        this.targetSelector.addGoal(2, new NearestAttackableTargetGoal<>(this, PlayerEntity.class, true));
    }
    static class AttackGoal extends MeleeAttackGoal {
        public AttackGoal(GingerBreadManEntity en) {
           super(en, 1.0D, true);
        }
  
        @Override
        public boolean canContinueToUse() {
           float f = this.mob.getBrightness();
           if (f >= 0.5F && this.mob.getRandom().nextInt(100) == 0) {
              this.mob.setLastHurtByMob((LivingEntity)null);
              return false;
           } else {
              return super.canContinueToUse();
           }
        }
  
        protected double getAttackReachSqr(LivingEntity attackTarget) {
           return (double)(4.0F + attackTarget.getBbWidth());
        }
    }

    @Override
    public CreatureAttribute getMobType() {
        return CreatureAttribute.UNDEAD;
    }

    @Override
    public net.minecraft.util.SoundEvent getHurtSound(DamageSource ds) {
        return (net.minecraft.util.SoundEvent) ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("entity.generic.hurt"));
    }

    @Override
    public net.minecraft.util.SoundEvent getDeathSound() {
        return (net.minecraft.util.SoundEvent) ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("entity.generic.death"));
    }
}